const app = getApp()
Page({

  /**
   * 页面的初始数据
   * type 1:任务通知 2:官方通知
   */
  data: {
    incomeStatistics: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
    app.userLoginReadyCallback = res => {
      this.getIncomeStatistics();
    }
    this.getIncomeStatistics();

  },
  getIncomeStatistics: function () {
    let _this = this;
    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/wechat/getIncomeStatistics',
      header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
      success: function (res) {
        console.log(res.data.object);

        if (res.data.status == 'ok') {
          _this.setData({
            incomeStatistics: res.data.object,
          })
          app.globalData.incomeStatistics = res.data.object
        } else {
          app.responseCheck(_this, res.data, '/pages/user/wallet/wallet')
        }
      },
      fail: function (e) {
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  tixianBtn: function (event) {
    var _this = this;

    //账号是否绑定
    var aliPayId = app.globalData.userInformation.aliPayId;
    if(!aliPayId) {
      wx.showModal({
        content: '请绑定支付账号，是否马上绑定？',
        confirmText: '是',
        cancelText: '否',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../userinformation/userinformation',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return;
    }

    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/wechat/checkAuditStatus',
      header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
      success: function (res) {
        console.log(res);
        if (res.data.status == 'ok') {
          
          // 提交审核
          if (res.data.object.auditstatus == 1) {
            wx.showModal({
              content: '身份认证正在审核，请等待',
              showCancel: false
            })
          } else if (res.data.object.auditstatus == 2) {
            // 审核成功
            wx.navigateTo({
              url: '../withdraw/withdraw',
            })
          } else if (res.data.object.auditstatus == 3){
            // 审核失败
            wx.navigateTo({
               url: '../auth/auth',
            })
            // wx.showModal({
            //   content: res.data.object.auditdesc ? '身份认证审核失败 : ' + res.data.object.auditdesc : '身份认证审核失败',
            //   showCancel: false,
            //   success: function(res) {
            //     wx.navigateTo({
            //       url: '../auth/auth',
            //     })
            //   }
            // })
          }else{
            wx.navigateTo({
              url: '../auth/auth',
            })
          }

        } else {
          app.responseCheck(_this, res.data, '/pages/user/wallet/wallet')
        }
      },
      fail: function (e) {
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  },
  earnBtn: function (e) {
    wx.switchTab({
      url: '../../public_tasks/task_list/task_list',
    })
  },
  cashBtn: function (e) {
    wx.navigateTo({
      url: '../account/account',
    })
  }
})